package com.company.project.controller;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.AdminLog;
import com.company.project.model.ShopLogin;
import com.company.project.model.request.QueryParams;
import com.company.project.service.AdminLogService;
import com.company.project.service.ShopLoginService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.List;

/**
* Created by CodeGenerator on 2020/04/21.
*/
@RestController
@RequestMapping("/log")
public class LogController {
    @Resource
    private AdminLogService adminLogService;
    @Autowired
    private ShopLoginService shopLoginService;


    @PostMapping("/delete")
    public Result delete(@RequestBody String id) {
        adminLogService.deleteOne(id);
        return ResultGenerator.genSuccessResult();
    }
    @PostMapping("/deleteAll")
    @Transactional
    public Result deleteAll(@RequestBody List<AdminLog> logList) {
        for(AdminLog log : logList) {
            adminLogService.deleteOne(log.getId());
        }
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/admin/list")
    public Result list(@RequestBody QueryParams queryParams) {
        if(queryParams == null) {
            return ResultGenerator.genFailResult("参数错误");
        }
        return adminLogService.list(queryParams);
    }

    @PostMapping("/shop/list")
    public Result shopList(@RequestBody QueryParams queryParams) {
        if(queryParams == null) {
            return ResultGenerator.genFailResult("参数错误");
        }
        return adminLogService.shopList(queryParams);
    }

    @GetMapping("/all")
    public Result all() {
        List<AdminLog> all = adminLogService.findAll();
        return ResultGenerator.genSuccessResult(all);
    }
    @GetMapping("/shop/all/{id}")
    public Result all(@PathVariable("id") Integer id) {
        ShopLogin shop = shopLoginService.findBy("shopId", id);
        Condition condition = new Condition(AdminLog.class);
        condition.createCriteria().andEqualTo("username", shop.getName());
        List<AdminLog> all = adminLogService.findByCondition(condition);
        return ResultGenerator.genSuccessResult(all);
    }
}
